﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    class DBProduct
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        public List<Product> GetProductList(Guid mkguid)
        {
            List<Product> pdlist = new List<Product>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var plist = from pd in _dc.UProduct
                                 where pd.PDmkguid == mkguid
                                 select new
                                 {
                                     pd.PDdatein,
                                     pd.PDguid,
                                     pd.PDlastmodify,
                                     pd.PDmkguid,
                                     pd.PDpicbig,
                                     pd.PDpicsmall,
                                     pd.PDstatus,
                                     pd.PDtitletagid
                                 };

                    foreach (var p in plist)
                    {
                        Product pt = new Product()
                        {
                            TITLE = TagFactory.GetTagOnly(p.PDtitletagid).TITLE,
                            TITLETAGID = p.PDtitletagid,
                            PDGUID = p.PDguid,
                            PICBIG = p.PDpicbig,
                            PICSMALL = p.PDpicsmall,
                            PDMKGUID = p.PDmkguid,
                            PDSTATUS = p.PDstatus
                        };
                        
                        pdlist.Add(pt);
                    }
                    

                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetProductList", ex.ToString());
            }
            return pdlist;
        }

        public Product GetProduct(Guid pdguid)
        {
            Product pd = new Product();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var up = _dc.UProduct.SingleOrDefault(upd => upd.PDguid == pdguid);

                    if (up != null)
                    {
                        pd.PDGUID = up.PDguid;
                        pd.PDMKGUID = up.PDmkguid;
                        pd.PICBIG = up.PDpicbig;
                        pd.PICSMALL = up.PDpicsmall;
                        pd.TITLETAGID = up.PDtitletagid;
                        pd.PDSTATUS = up.PDstatus;
                        pd.TITLE = TagFactory.GetTagOnly(up.PDtitletagid).TITLE;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetProduct", ex.ToString());
            }
            return pd;
        }



        public List<Specification> GetSpecList(Guid pdguid)
        {
            List<Specification> speclist = new List<Specification>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var slist = from s in _dc.USpecification
                                where s.SPpdguid == pdguid
                                select new
                                {
                                    s.SPguid,
                                    s.SPpdguid,
                                    s.SPstatus,
                                    s.SPstock,
                                    s.SPtitletagid
                                };

                    foreach (var s in slist)
                    {
                        Specification spec = new Specification()
                        {
                            SID = s.SPguid,
                            STOCK = s.SPstock,
                            TITLETAGID = s.SPtitletagid,
                            TITLE = TagFactory.GetTagOnly(s.SPtitletagid).TITLE,
                            SPSTATUS = s.SPstatus,
                            PDGUID = s.SPpdguid
                        };

                        speclist.Add(spec);
                    }

                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetSpecList", ex.ToString());
            }
            return speclist;
        }

        public Specification GetSpec(Guid specguid)
        {
            Specification sp = new Specification();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var us = _dc.USpecification.SingleOrDefault(s => s.SPguid == specguid);

                    if (us != null)
                    {
                        sp.SID = us.SPguid;
                        sp.PDGUID = us.SPpdguid;
                        sp.STOCK = us.SPstock;
                        sp.TITLETAGID = us.SPtitletagid;
                        sp.SPSTATUS = us.SPstatus;
                        sp.TITLE = TagFactory.GetTagOnly(us.SPtitletagid).TITLE;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetSpec", ex.ToString());
            }
            return sp;
        }

        public void UpdateSpecification(Specification spec)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    USpecification us = _dc.USpecification.FirstOrDefault(s => s.SPguid == spec.SID);
                    if (us != null)
                    {
                        us.SPstock = spec.STOCK;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateSpecification", ex.ToString());
                }
            }
        }

        public void UpdateSpecificationStatus(Specification spec)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    USpecification us = _dc.USpecification.FirstOrDefault(s => s.SPguid == spec.SID);
                    if (us != null)
                    {
                        us.SPstatus = spec.SPSTATUS;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateSpecificationStatus", ex.ToString());
                }
            }
        }

    }
}